package cn.xscrum.vtsp.controller;

import java.util.List;

import cn.xscrum.vtsp.domain.VtspAreas;
import cn.xscrum.vtsp.service.IVtspAreasService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.xscrum.common.annotation.Log;
import cn.xscrum.common.enums.BusinessType;
import cn.xscrum.vtsp.domain.VtspRecipient;
import cn.xscrum.vtsp.service.IVtspRecipientService;
import cn.xscrum.common.core.controller.BaseController;
import cn.xscrum.common.core.domain.AjaxResult;
import cn.xscrum.common.utils.poi.ExcelUtil;
import cn.xscrum.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 服务对象信息Controller
 *
 * @author xscrum
 * @date 2021-10-01
 */
@Controller
@RequestMapping("/vtsp/recipient")
public class VtspRecipientController extends BaseController {
    private String prefix = "vtsp/recipient";

    @Autowired
    private IVtspRecipientService vtspRecipientService;
    @Autowired
    private IVtspAreasService areasService;

    @RequiresPermissions("vtsp:recipient:view")
    @GetMapping()
    public String recipient() {
        return prefix + "/recipient";
    }

//    /**
//     * 查询服务对象信息列表
//     */
//    @RequiresPermissions("vtsp:recipient:list")
//    @PostMapping("/list")
//    @ResponseBody
//    public TableDataInfo list(VtspRecipient vtspRecipient) {
//        startPage();
//        List<VtspRecipient> list = vtspRecipientService.selectVtspRecipientList(vtspRecipient);
//        return getDataTable(list);
//    }

    /**
     * 按活动id查服务对象
     */
    @RequiresPermissions("vtsp:recipient:list")
    @PostMapping("/listById/{activityId}")
    @ResponseBody
    public TableDataInfo listById(@PathVariable("activityId") Long activityId) {
        startPage();
        VtspRecipient vtspRecipient = new VtspRecipient();
        vtspRecipient.setActivityId(activityId);
        List<VtspRecipient> list = vtspRecipientService.selectVtspRecipientList(vtspRecipient);
        return getDataTable(list);
    }

    /**
     * 导出服务对象信息列表
     */
    @RequiresPermissions("vtsp:recipient:export")
    @Log(title = "服务对象信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VtspRecipient vtspRecipient) {
        List<VtspRecipient> list = vtspRecipientService.selectVtspRecipientList(vtspRecipient);
        ExcelUtil<VtspRecipient> util = new ExcelUtil<VtspRecipient>(VtspRecipient.class);
        return util.exportExcel(list, "recipient");
    }

    /**
     * 新增服务对象国家信息
     */
    @GetMapping("/add/{activityId}")
    public String add(@PathVariable Long activityId, Model model) {
        List<VtspAreas> areas = areasService.selectVtspAreasList(null);
        model.addAttribute("areas", areas);
        model.addAttribute("acticityId",activityId);
        return prefix + "/add";
    }

    /**
     * 新增保存服务对象信息
     */
    @RequiresPermissions("vtsp:recipient:add")
    @Log(title = "服务对象信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VtspRecipient vtspRecipient) {
        Long recipientCountry = vtspRecipient.getRecipientCountry();
        VtspAreas vtspAreas = areasService.selectVtspAreasById(recipientCountry);
        vtspRecipient.setCountryName(vtspAreas.getAreasName());
        return toAjax(vtspRecipientService.insertVtspRecipient(vtspRecipient));

    }

    /**
     * 修改服务对象信息
     */
    @GetMapping("/edit/{recipientId}")
    public String edit(@PathVariable("recipientId") Long recipientId, ModelMap mmap) {
        List<VtspAreas> areas = areasService.selectVtspAreasList(null);
        mmap.addAttribute("areas", areas);
        VtspRecipient vtspRecipient = vtspRecipientService.selectVtspRecipientById(recipientId);
        mmap.put("vtspRecipient", vtspRecipient);
        return prefix + "/edit";
    }

    /**
     * 修改保存服务对象信息
     */
    @RequiresPermissions("vtsp:recipient:edit")
    @Log(title = "服务对象信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VtspRecipient vtspRecipient) {
        Long recipientCountry = vtspRecipient.getRecipientCountry();
        VtspAreas vtspAreas = areasService.selectVtspAreasById(recipientCountry);
        vtspRecipient.setCountryName(vtspAreas.getAreasName());
        return toAjax(vtspRecipientService.updateVtspRecipient(vtspRecipient));
    }

    /**
     * 删除服务对象信息
     */
    @RequiresPermissions("vtsp:recipient:remove")
    @Log(title = "服务对象信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(vtspRecipientService.deleteVtspRecipientByIds(ids));
    }

    /**
     * 导入数据
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<VtspRecipient> util = new ExcelUtil<VtspRecipient>(VtspRecipient.class);
        return util.importTemplateExcel("服务对象信息");
    }

    /**
     * 活动导入
     */
    @RequiresPermissions("vtsp:recipient:import")
    @Log(title = "活动导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importList")
    @ResponseBody
    public AjaxResult importList(MultipartFile file) throws Exception
    {
        //创建Excel工具类
        ExcelUtil<VtspRecipient> util = new ExcelUtil<VtspRecipient>(VtspRecipient.class);
        //选择导入文件
        List<VtspRecipient> list = util.importExcel(file.getInputStream());
        //导入文件
        String message = vtspRecipientService.importList(list);
        return AjaxResult.success(message);
    }
}
